package com.ti.demo.configuration;

import com.ti.demo.entity.WeatherInfo;
import com.ti.demo.entity.WeatherRequest;
import com.ti.demo.entity.WeatherResponse;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.web.client.RestClient;

import java.util.function.Function;

/**
 * @description: com.ti.demo.configuration
 * @Author Triagen
 * @Date 2025/10/9 11:01
 */
@Configuration
public class WeatherConfiguration {

    @Bean
    public RestClient getRestClient(ObjectProvider<RestClient.Builder> restClientBuilderProvider) {
        return restClientBuilderProvider.getIfAvailable(RestClient::builder).baseUrl("https://weather.cma.cn/api").build();
    }

    // 使用 Function Bean，此代码注释
//    @Bean("getWeatherInfoToolCallback")
//    public ToolCallback getWeatherInfoToolCallback(RestClient restClient) {
//        return FunctionToolCallback.builder("getWeatherInfoToolCallback", (Function<WeatherRequest, WeatherInfo>) weatherRequest -> {
//                    WeatherResponse weatherResponse = restClient.get().uri("/now/" + weatherRequest.getCityId()).retrieve().body(WeatherResponse.class);
//                    return weatherResponse.getData();
//                })
//                .inputType(WeatherRequest.class)
//                .description("Get weather info by cityId")
//                .build();
//    }

    @Description("get weather info by cityId")   // 如果去掉此注解，则默认拆分beanName，即描述信息为 get weather info
    @Bean
    public Function<WeatherRequest, WeatherInfo> getWeatherInfo(RestClient restClient){
        return weatherRequest -> {
            WeatherResponse weatherResponse = restClient.get().uri("/now/" + weatherRequest.getCityId()).retrieve().body(WeatherResponse.class);
            return weatherResponse.getData();
        };
    }


}
